package com.tripspins.app.helper;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.AsyncTask;

import com.tripspins.app.R;
import com.tripspins.app.bo.Attachment;
import com.tripspins.app.exception.NoConnectionException;
import com.tripspins.app.exception.OfflineModeException;
import com.tripspins.app.interfaces.OnTaskCompleted;

import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

import ch.boye.httpclientandroidlib.HttpResponse;
import ch.boye.httpclientandroidlib.client.HttpClient;
import ch.boye.httpclientandroidlib.client.methods.HttpPost;
import ch.boye.httpclientandroidlib.impl.client.DefaultHttpClient;

/**
 * Created by Joao on 9/23/2014.
 */
public class SocialHelper {

    Attachment attachment;
    OnTaskCompleted activity;

    public void shareOnFacebook(long idTrip, long idPin, Attachment att, OnTaskCompleted activity)
            throws NoConnectionException, OfflineModeException
    {

        boolean online = PreferencesHelper.getSharedPreference((Activity)activity, "pref_key_internet_connection",false);
        if(!online) throw new OfflineModeException();
        online = DeviceCheckHelper.isNetworkConnected((Activity)activity);
        if(!online) throw new NoConnectionException();

        String url = ((Activity) activity).getString(R.string.WS_ADDRESS_ATTACHMENT_SHARE);
        this.activity = activity;
        attachment = att;
        HttpPostShare httpPostShare = new HttpPostShare(((Activity) activity).getApplicationContext());
        String[] params = new String[]{String.valueOf(idTrip), String.valueOf(idPin), String.valueOf(att.getId()),  url};
        httpPostShare.execute(params);

    }

    class HttpPostShare extends AsyncTask<String,Void,Attachment>
    {
        Context context;
        public HttpPostShare(Context context){
            this.context = context;
        }

        @Override
        protected Attachment doInBackground(String... strings) {
            String result = null;

            try{
                long tripId = Long.valueOf(strings[0]);
                long pinId = Long.valueOf(strings[1]);
                long attachmentId = Long.valueOf(strings[2]);
                String strUrl = strings[3];

                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(strUrl);

                httpPost.addHeader("id_trip", String.valueOf(tripId));
                httpPost.addHeader("id_pin", String.valueOf(pinId));
                httpPost.addHeader("id_attachment", String.valueOf(attachmentId));


                HttpResponse httpResponse = httpclient.execute(httpPost);
                InputStream inputStream = httpResponse.getEntity().getContent();
                result = convertInputStreamToString(inputStream);

                JSONObject jsonAttachment = new JSONObject(result);
                attachment.setPublicFilePath(jsonAttachment.getString("txt_public_file_path"));


            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return attachment;


        }

        @Override
        protected void onPostExecute(Attachment attachment) {
            super.onPostExecute(attachment);
            activity.onTaskCompleted(attachment);
        }
        private String convertInputStreamToString(InputStream inputStream) throws IOException {
            BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
            String line = "";
            String result = "";
            while((line = bufferedReader.readLine()) != null)
                result += line;

            inputStream.close();
            return result;

        }
    }


}
